<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\DBCont;
use think\Exception;

class VotePlayer extends BaseModel
{
    public function vote(array $player_ids, $user_id){
        // 投票异常
        $vote_id = model('Player')->whereIn('id', $player_ids)->group('vote_id')->column('vote_id');
        if (count($vote_id) != 1){
            $this->error = '操作有误，存在多个活动';
            return false;
        }
        // 活动状态
        $vote_id = $vote_id[0];
        $data = model('VoteInfo')->where('id', $vote_id)->find();
        if ($data['status_desc'] != DBCont::SUCCESS_DESC){
            $this->error = '该活动并非进行中，不能投票';
            return false;
        }
        // 判断是否已投票
        $time = strtotime(date('Y-m-d'));
        $r = model('VotePlayer')->whereIn('player_id', $player_ids)->where('user_id', $user_id)->where('vote_time', $time)->value('player_id');
        if (!empty($r)){
            $number = model('Player')->where('id', $r)->value('number');
            $this->error = $number.'号，您已过，看看别的选手吧！';
            return false;
        }
        $nowNumber = count($player_ids);
        if ($data['period'] == '固定次数') {
            $number = model('VotePlayer')->where('vote_id', $vote_id)->where('user_id', $user_id)->count();
        } else {
            // 获取今天投票的数量
            $number = model('VotePlayer')->where('vote_id', $vote_id)->where('user_id', $user_id)->where('vote_time', $time)->count();
        }
        switch ($data['vote_type']){
            case '单选投票' :
                if ($number > 0){
                    $this->error = $data['period'] == '固定次数'? '您已投过票了':'您已投过票了，明天再来吧';
                    return false;
                }
                if ($nowNumber != 1){
                    $this->error = $data['period'] == '固定次数'? '您已投过票了':'您已投过票了，明天再来吧';
                    return false;
                }
                break;
            case '必选投票' :
                if ($nowNumber != $data['vote_max']){
                    $this->error = '必须选择'.$data['vote_max'].'个票数才能投票';
                    return false;
                }
                if ($number > 0){
                    $this->error = $data['period'] == '固定次数'? '您已投过票了':'您已投过票了，明天再来吧';
                    return false;
                }
                break;
            case '多选投票' :
                if ($number >= $data['vote_max']){
                    $this->error = '您的可投票数已经用完';
                    return false;
                }
                if ($nowNumber > $data['vote_max']){
                    $this->error = '最多只能选择'.$data['vote_max'].'张票';
                    return false;
                }
                if ($nowNumber < $data['vote_min']){
                    $this->error = '至少要选择选择'.$data['vote_min'].'张票';
                    return false;
                }
                if ($number + $nowNumber > $data['vote_max']){
                    if ($data['vote_max'] - $number < $data['vote_min']){
                        $this->error = $data['period'] == '固定次数'? '您已投过票了':'您已投过票了，明天再来吧';
                        return false;
                    }
                    $this->error = '选择票数过多，最多只能再投'.($data['vote_max']-$number).'张票';
                    return false;
                }
                break;
            default :
        }
        $this->startTrans();
        try{
            $arr = [
                'user_id' => $user_id,
                'vote_id' => $vote_id,
                'vote_time' => strtotime(date('Y-m-d')),
            ];
            foreach ($player_ids as $id){
                $arr['player_id'] = $id;
                $r = $this->createAdd($arr);
                if ($r === false){
                    \exception($this->getError());
                }
            }
            $this->commit();
            return true;
        } catch (\Exception $e){
            $this->rollback();
            return false;
        }
    }

    /**
     * 统计某区间的投票活动的投票数量
     * @param $vote_id
     * @param $startTime
     * @param $endTime
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function statistics($vote_id, $startTime, $endTime){
        $vote = $this->where('vote_id', $vote_id)
            ->whereBetween('vote_time', [$startTime-1, $endTime+1])
            ->field(['vote_time as key', 'count(*) as number'])->group('vote_time')->select();
        return $vote;
    }

    /**
     * 统计某个时间区间的选手的投票总量
     * @param $player_id
     * @param $startTime
     * @param $endTime
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function statisticsPlayer($player_id, $startTime, $endTime){
        $player = $this->where('player_id', $player_id)
            ->whereBetween('vote_time', [$startTime-1, $endTime+1])
            ->field(['vote_time as key', 'count(*) as number'])->group('vote_time')->select();
        return $player;
    }
}